package cn.net.isir.controller.common;

import cn.net.isir.common.CacheManager;
import cn.net.isir.common.ResultObject;
import cn.net.isir.common.sms.FailMobileInfo;
import cn.net.isir.common.sms.SMSResult;
import cn.net.isir.common.sms.SMSUtils;
import cn.net.isir.entity.UserInfo;
import cn.net.isir.service.UserInfoService;
import cn.net.isir.utils.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author liuxubo
 * @filename UserSMSController.java
 * @package cn.net.isir.controller.common
 * @email lxb@isir.net.cn
 * @date 2018/12/28
 * @comment 发送短信
 * @since 1.8
 */
@RestController
@RequestMapping("/sms")
public class UserSMSController {

    private static Logger logger = Logger.getLogger(UserSMSController.class);

    @Resource
    private UserInfoService userInfoService;


    /**
     * 获取短信验证码(5分钟有效)
     *
     * @param mobile 手机号
     * @return 结果
     */
    @RequestMapping(value = "/sendVerCode/{type}/{mobile}", method = RequestMethod.POST)
    public ResultObject sendVerCode(@PathVariable String type, @PathVariable String mobile) {
        // 业务校验
        UserInfo info = new UserInfo();
        info.setTel(mobile);
        int count = userInfoService.selectCount(info);

        // 1.注册的时候,已经注册的手机号无法发送验证码(type = register)
        if ("register".equals(type)) {
            if (count > 0) {
                return new ResultObject(511, "fail", "mobile is already exist.");
            }
        }

        // 2.登录的时候,未注册的手机号无法发送验证码(type = login)
        if ("login".equals(type)) {
            if (count <= 0) {
                return new ResultObject(512, "fail", "mobile is not exist.");
            }
        }

        // 3.重置密码的时候,未注册的手机号无法发送验证码(type = login)
        if ("reset".equals(type)) {
            if (count <= 0) {
                return new ResultObject(512, "fail", "mobile is not exist.");
            }
        }

        String verCode = StringUtils.randomNumber(5, false);
        logger.info("sms code:----------[" + mobile + "]---------[" + verCode + "]");
        SMSResult result = SMSUtils.sendVerCode(mobile, verCode);
//        SMSResult result = null;
        if (false) {
            result = new SMSResult();//SMSUtils.sendVerCode(mobile, verCode);
            result.setResult(200);
        }
        if (result.getResult() == 200) {
            //保存验证码到缓存,缓存时间5*60秒
            CacheManager.setVerCode(mobile, type, verCode, 5 * 60 + 10);
            return new ResultObject(200, "success", mobile);
        }
        //对同一个手机号， 30秒 内发送短信条数不超过 1条
        //对同一个手机号， 1小时 内发送短信条数不超过 5条
        //对同一个手机号， 1个自然日 内发送短信条数不超过 10条
        FailMobileInfo failMobileInfo = result.getFailMobiles().get(0);
        int failCode = Integer.parseInt(failMobileInfo.getFailCode());
        if (failCode == 1023 || failCode == 1024 || failCode == 1025) {
            return new ResultObject(501, "验证码发送太频繁,请稍后再试! (1小时内最多获取5次,1天最多获取10次)", null);
        }
        return new ResultObject(500, "fail", null);
    }
}
